var app = getApp();
Page({
    data: {
        isShow:false,           // 用户是否授权
        src: '../../img/banner.png',
        winHeight: "",          // 窗口高度
        tabbarList:[],          // 分类列表
        currentTab: 0,          // 预设当前项的值
        currentTabId:0,         // 当前tab ID     
        scrollLeft: 0,          // tab标题的滚动条位置
        goodsList:[],           // 商品数据列表

        noData: 0,              // 没有数据

        expertList: [{ //假数据
            item_img: "avatar.png",
            tittle: "描述",
            tag: "欢顔",
            money: "知名情感博主",
            fouce: 134
        }],
		
		user_info:"",			// 用户详情

    },
	showDialog(){
		this.dialog.showDialog();
	},
    // 滚动切换标签样式
    switchTab: function(e) {
        this.setData({
            currentTab: e.detail.current,
            goodsList:[],
        });
        this.checkCor();
        this.getItemGoodsList(this.data.tabbarList[e.detail.current].id)
    },
    // 点击标题切换当前页时改变样式
    swichNav: function(e) {
        var current = e.target.dataset.current;
        var id = e.target.dataset.id;
        
        if (this.data.currentTab == current) {
            return false;
        } else {
            this.setData({
                currentTab: current
            })
        }
    },
    getItemGoodsList(id){
        var self = this;
        var userData = this.data.userData;
        var noData = this.data.noData;
        var datas = this.data.goodsList;
       	var user_info = this.data.user_info;

		wx.request({
			url: app.url.requestUrl + '/goods/freelist',
			method: 'POST',
			data: {
				openid: user_info.openid,
				token: user_info.token,
				cate_id: id
			},
			success: function (data) {
				var list = data.data;
				if (list.data || datas.length) {
					noData = 0;
				} else {
					noData = 1;
				} 
				self.setData({
					noData: noData
				})
				if (data.data.code == 1) {
					self.setData({
						goodsList: list.data.data
					})
				}else{
					if (data.data.code == -1 || data.data.code == 404) {
						self.loginOverdue()
					}
				}
                setTimeout(function () {
                    wx.stopPullDownRefresh()
                    wx.hideLoading()
                }, 300)
			}
		})
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function() {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    bindGetUserInfo(){
        this.login.hideModalBox()
    },
	// 获取切换标签 tab
	getTabbar(userInfo){
		var self = this;
		wx.showLoading({ title: '加载中' })
		wx.request({
			url: app.url.requestUrl + '/goods/cate',
			method: 'POST',
			data: {
				openid: userInfo.openid,
				token: userInfo.token
			},
			success: function (data) {
				var list = data.data;
				if (list.code == 1) {
					self.setData({
						tabbarList: list.data,
					})
					self.getItemGoodsList(list.data[0].id)
				}
			}
		})
	},
    getBanner(res) {
        var self = this;
        wx.showLoading({ title: '加载中' })
        wx.request({
            url: app.url.requestUrl + '/index/getBanner',
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
                type: 3
            },
            success: function (resbanner) {
                if (resbanner.data.code == 1) {
                    self.setData({
                        src: resbanner.data.data.img
                    })
                } else {
                    if (resbanner.data.code == -1 || resbanner.data.code == 404) {
                        self.loginOverdue();
                    }
                }
            }
        })
    },
    onLoad: function() {
        var self = this;
		
		// this.dialog = this.selectComponent("#dialog");
        // this.login = this.selectComponent("#login");

		//  高度自适应
		wx.getSystemInfo({
			success: function (res) {
				var clientHeight = res.windowHeight,
					clientWidth = res.windowWidth,
					rpxR = 750 / clientWidth;
				var calc = clientHeight * rpxR - 180;
				self.setData({
					winHeight: calc
				});
			}
		});


		wx.getStorage({
			key: 'user',
			success: function (res) {
                self.getBanner(res)
				self.getTabbar(res.data)
				self.setData({ user_info: res.data })
			},
			fail() {
				wx.login({
					success: res => {
						wx.request({
							url: app.url.requestUrl + '/login/login',
							method: 'POST',
							data: {
								code: res.code
							},
							success: function (data) {
								if (data.data.code == 1) {
									wx.setStorage({
										key: 'user',
										data: data.data.data
									})
									self.setData({ user_info: data.data.data })
									self.getTabbar(data.data.data)	
								}
							}
						})
					}
				})
			}
		})
    },
	// 登录过期状态
	loginOverdue(e) {
		var self = this;
		wx.removeStorage({
			key: 'user',
			success: function (res) {
				self.onLoad();
			},
		})
	},
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            goodsList:[]
        })
        this.onLoad();
    },
	// 弹窗点击 取消 按钮触发函数
	__cancelEvent(){
		this.dialog.hideDialog();
	},
	// 弹窗点击 确认 按钮触发函数
	__confirmEvent(){
		this.dialog.hideDialog();
	},
    footerTap: app.footerTap
})